package com.sufen.seckill.study.FaceCoding;

import java.util.*;

public class TreeInput {
    public static class TreeNode{
        int val;
        TreeNode left;
        TreeNode right;
        public TreeNode(){}
        public TreeNode(int val){
            this.val=val;
        }
    }

    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);

//        输入：[1,2,3,null,null,6,7]

        String tmp=in.nextLine();

        String s=tmp.substring(1,tmp.length()-1);

        String[] ss=s.split(",");

        int n=ss.length;

        int[] arr=new int[n];

        for(int i=0;i<n;i++){
            arr[i]=ss[i].equals("null")?-1:Integer.parseInt(ss[i]);
        }
//        for(int i=0;i<n;i++){
//            System.out.println(arr[i]);
//        }
        TreeNode root=buildTree(arr);

        List<List<Integer>> list=help(root);

        for(List<Integer> l:list){
            System.out.println(l);
        }

    }

    public static TreeNode buildTree(int[] arr){
//        输入：[1,2,3,null,null,6,7]
//        转化成int[]再输入
        ArrayList<TreeNode> list=new ArrayList<>();

        int n=arr.length;

        TreeNode root=null;

        Collections.fill(list,null);

        for(int i=0;i<n;i++){
            TreeNode node=null;
            if(arr[i]!=-1){
                node=new TreeNode(arr[i]);
            }
            list.add(i,node);
            if(i==0){
                root=node;
            }
        }

        for(int i=0;2*i<arr.length;i++){
            if(list.get(i)!=null){
                list.get(i).left=list.get(2*i+1);
                list.get(i).right=list.get(2*i+2);
            }
        }
        return root;
    }

    private static List<List<Integer>> help(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        LinkedList<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while(queue.size() > 0){
            List<Integer> tmp = new ArrayList<>();
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                TreeNode node = queue.poll();
                tmp.add(node.val);
                if(node.left != null){
                    queue.add(node.left);
                }
                if(node.right != null){
                    queue.add(node.right);
                }
            }
            res.add(tmp);
        }
        return res;
    }
}
